﻿using System.Collections.Generic;
using System.Linq;
using DTO;

namespace DAO
{
    public class LogoDAO
    {
        public static List<LogoDTO> getAll()
        {
            List<LogoDTO> result = new List<LogoDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.LOGOs.Where(dm => dm.Deleted.Equals(0)).ToList();

            foreach (var ds in query)
            {
                LogoDTO dto = new LogoDTO();

                dto.MaLogo = ds.MaLogo;
                dto.TenLogo = ds.TenLogo;
                dto.DuongDanLogo = ds.DuongDanLogo;
                
                result.Add(dto);
            }

            return result;
        }

        public static LogoDTO getAlongCode(int maLogo)
        {
            LogoDTO result = new LogoDTO();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.LOGOs.Where(dm => dm.Deleted.Equals(0) &&
                dm.MaLogo == maLogo).Single();

            result.MaLogo = query.MaLogo;
            result.TenLogo = query.TenLogo;
            result.DuongDanLogo = query.DuongDanLogo;

            return result;
        }

        public static void Insert(LogoDTO _insert)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            LOGO logo = new LOGO();
            logo.TenLogo = _insert.TenLogo;
            logo.DuongDanLogo = _insert.DuongDanLogo;
            logo.Deleted = _insert.Delete;
            
            dbRaoVat.LOGOs.InsertOnSubmit(logo);
            dbRaoVat.SubmitChanges();
        }

        public static void Delete(int maLogo)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            LOGO _delete = (from n in dbRaoVat.LOGOs
                                 where n.MaLogo == maLogo && n.Deleted == 0
                                 select n).Single();

            _delete.Deleted = 1;
            dbRaoVat.SubmitChanges();
        }

        public static void Update(LogoDTO dto)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            LOGO _update = (from n in dbRaoVat.LOGOs
                            where n.MaLogo == dto.MaLogo && n.Deleted == 0
                            select n).Single();

            _update.TenLogo = dto.TenLogo;
            _update.DuongDanLogo = dto.DuongDanLogo;
            
            dbRaoVat.SubmitChanges();
        }
    }
}
